package net.zhengxinyun.performance.data;

import com.alibaba.fastjson.JSONArray;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.Util.WorkbookUtil;
import net.zhengxinyun.performance.entity.MeasurementInformationEntity;
import net.zhengxinyun.performance.entity.QuestionEntity;
import net.zhengxinyun.performance.filter.EvaluateUserFilter;
import net.zhengxinyun.performance.mapper.login.EvaluateUserEntityMapper;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by hyp1202 on 2019/1/14.
 */
@Component
public class MeasurementData {

    private EvaluateUserEntityMapper evaluateUserEntityMapper;

    @Autowired
    public MeasurementData(EvaluateUserEntityMapper evaluateUserEntityMapper) {
        this.evaluateUserEntityMapper = evaluateUserEntityMapper;
    }

    private static final String COL_NAME = "测评名";
    private static final String COL_TYPE = "测评类型";
    private static final String COL_TEST_OBJ = "测评对象";
    private static final String COL_STAR_TIME = "开始时间";
    private static final String COL_END_TIME = "结束时间";
    private static final String COL_A_SCORE = "a分数";
    private static final String COL_B_SCORE = "b分数";
    private static final String COL_C_SCORE = "c分数";
    private static final String COL_D_SCORE = "d分数";
    private static final String COL_E_SCORE = "e分数";
    private static final String COL_F_SCORE = "f分数";

    private static final String COL_NUMBER = "题目序号";
    private static final String COL_QUESTION_TYPE = "题目类型";
    private static final String COL_QUESTION = "题目";
    private static final String COL_OPTION_A ="选项a";
    private static final String COL_OPTION_B ="选项b";
    private static final String COL_OPTION_C ="选项c";
    private static final String COL_OPTION_D ="选项d";
    private static final String COL_OPTION_E = "选项e";
    private static final String COL_OPTION_F = "选项f";
    private static final String COL_REQUIRED = "必选属性";

    private static final String[] LABLE_ZERO = {"测评名","测评类型","测评对象","开始时间","结束时间","a分数","b分数","c分数","d分数",
            "e分数","f分数"};
    private static final String[] LABLE_ONE = {"题目序号","题目类型","题目","选项a","选项b","选项c","选项d","选项e","选项f","必选属性"};

    public Map<String,Object> improtMeasurementInformation(String filePath) throws Exception{
        Map<String,Object> map = new LinkedHashMap<>();
        List<QuestionEntity> questionList = new ArrayList<>();
        List<MeasurementInformationEntity> result = new ArrayList<>();
        List<String> errors = new ArrayList<>();
        Workbook workbook = WorkbookUtil.getWorkbook(filePath);
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Cell> iteratorZero = sheet.getRow(0).cellIterator();
        int length = 0;
        List<String> lablesListZero = new ArrayList<>();

        Sheet sheet1 = workbook.getSheetAt(1);
        Iterator<Cell> iteratorOne = sheet1.getRow(0).cellIterator();
        int lengthOne = 0;
        List<String> lablesListOne = new ArrayList<>();

        while (iteratorZero.hasNext()){
            String str = iteratorZero.next().getStringCellValue();
            if (!"".equals(str)){
                lablesListZero.add(str);
                if (length > 11){
                    throw new Exception("请选择正确模板上传");
                }
                length++;
            }
        }

        while (iteratorOne.hasNext()){
            String str = iteratorOne.next().getStringCellValue();
            if (!"".equals(str)){
                lablesListOne.add(str);
                if (lengthOne > 10){
                    throw new Exception("请选择正确模板上传");
                }
                lengthOne++;
            }
        }

        if (!Util.arrayEquals(lablesListZero.toArray(),LABLE_ZERO)){
            throw new Exception("请选择正确模板上传");
        }

        if (!Util.arrayEquals(lablesListOne.toArray(),LABLE_ONE)){
            throw new Exception("请选择正确模板上传");
        }

        for (int rowIdx = 2;rowIdx <= sheet.getLastRowNum();rowIdx++){
            Row row = sheet.getRow(rowIdx);
            if (WorkbookUtil.isRowEmpty(row)){
                continue;
            }
            try {
                MeasurementInformationEntity entityZero = new MeasurementInformationEntity();
                String name = WorkbookUtil.getStringCellValue(row,COL_NAME);
                if (StringUtils.isBlank(name)){
                    throw new Exception("测评名不能为空");
                }else {
                    entityZero.setName(name);
                }
                String type = WorkbookUtil.getStringCellValue(row,COL_TYPE);
                if (StringUtils.isBlank(type)){
                    throw new Exception("测评类型不能为空");
                }else if ("指定对象".equals(type)){
                    entityZero.setType("0");
                }else if ("未指定对象".equals(type)){
                    entityZero.setType("1");
                } else {
                    throw new Exception("测评类型有误");
                }
                String testObj = WorkbookUtil.getStringCellValue(row,COL_TEST_OBJ);
                if ("0".equals(type) && StringUtils.isBlank(testObj)){
                    throw new Exception("测评对象不能为空");
                }else {
                    entityZero.setTestObj(testObj);
                }
                String startTime = WorkbookUtil.getStringCellValue(row,COL_STAR_TIME);
                if (StringUtils.isBlank(startTime)){
                    throw new Exception("开始时间不能为空");
                }else {
                    entityZero.setStartTime(startTime);
                }
                String endTime = WorkbookUtil.getStringCellValue(row,COL_END_TIME);
                if (StringUtils.isBlank(endTime)){
                    throw new Exception("结束时间不能为空");
                }else {
                    entityZero.setEndTime(endTime);
                }
                String a = WorkbookUtil.getStringCellValue(row,COL_A_SCORE);
                if (StringUtils.isBlank(a)){
                    throw new Exception("a分数不能为空");
                }else {
                    entityZero.setScoreA(Double.valueOf(a));
                }
                String b = WorkbookUtil.getStringCellValue(row,COL_B_SCORE);
                if (StringUtils.isBlank(b)){
                    throw new Exception("b分数不能为空");
                }else {
                    entityZero.setScoreB(Double.valueOf(b));
                }
                String c = WorkbookUtil.getStringCellValue(row,COL_C_SCORE);
                if (StringUtils.isBlank(c)){
                    throw new Exception("c分数不能为空");
                }else {
                    entityZero.setScoreC(Double.valueOf(c));
                }
                String d = WorkbookUtil.getStringCellValue(row,COL_D_SCORE);
                if (StringUtils.isBlank(d)){
                    throw new Exception("d分数不能为空");
                }else {
                    entityZero.setScoreD(Double.valueOf(d));
                }
                String e = WorkbookUtil.getStringCellValue(row,COL_E_SCORE);
                if (StringUtils.isBlank(e)){
                    throw new Exception("e分数不能为空");
                }else {
                    entityZero.setScoreE(Double.valueOf(e));
                }
                String f = WorkbookUtil.getStringCellValue(row,COL_F_SCORE);
                if (StringUtils.isBlank(f)){
                    throw new Exception("f分数不能为空");
                }else {
                    entityZero.setScoreF(Double.valueOf(f));
                }

                entityZero.setStatus("0");
                String[] obj = testObj.split("、");
                EvaluateUserFilter filter = new EvaluateUserFilter();
                int totalPeople = 0;
                for (String str:obj) {
                    filter.setDepartment(str);
                    totalPeople += evaluateUserEntityMapper.queryForCount(filter);
                }
                entityZero.setTotalPeople(totalPeople+"");
                result.add(entityZero);
            }catch (Exception e){
                errors.add("第"+rowIdx+"行:"+e.getMessage());
            }
        }

        for (int rowIdx = 2;rowIdx <= sheet1.getLastRowNum();rowIdx++){
            Row row = sheet1.getRow(rowIdx);
            if (WorkbookUtil.isRowEmpty(row)){
                continue;
            }
            try {
                QuestionEntity questionEntity = new QuestionEntity();
                String serialNumber = WorkbookUtil.getStringCellValue(row,COL_NUMBER);
                if (StringUtils.isBlank(serialNumber)){
                    throw new Exception("题目序号不能为空");
                }else {
                    questionEntity.setSerialNumber(Double.valueOf(serialNumber));
                }
                String type = WorkbookUtil.getStringCellValue(row,COL_QUESTION_TYPE);
                if (StringUtils.isBlank(type)){
                    throw new Exception("题目类型不能为空");
                }else if ("单选题".equals(type)){
                    questionEntity.setType("0");
                }else if ("多选题".equals(type)){
                    questionEntity.setType("2");
                }else if ("简答题".equals(type)){
                    questionEntity.setType("1");
                }else {
                    throw new Exception("题目类型有误");
                }
                String question = WorkbookUtil.getStringCellValue(row,COL_QUESTION);
                if (StringUtils.isBlank(question)){
                    throw new Exception("题目不能为空");
                }else {
                    questionEntity.setQuestion(question);
                }
                String optionA = WorkbookUtil.getStringCellValue(row,COL_OPTION_A);
                if (StringUtils.isNotBlank(optionA)){
                    questionEntity.setOptionA(optionA);
                }
                String optionB = WorkbookUtil.getStringCellValue(row,COL_OPTION_B);
                if (StringUtils.isNotBlank(optionB)){
                    questionEntity.setOptionB(optionB);
                }
                String optionC = WorkbookUtil.getStringCellValue(row,COL_OPTION_C);
                if (StringUtils.isNotBlank(optionC)){
                    questionEntity.setOptionC(optionC);
                }
                String optionD = WorkbookUtil.getStringCellValue(row,COL_OPTION_D);
                if (StringUtils.isNotBlank(optionD)){
                    questionEntity.setOptionD(optionD);
                }
                String optionE = WorkbookUtil.getStringCellValue(row,COL_OPTION_E);
                if (StringUtils.isNotBlank(optionE)){
                    questionEntity.setOptionE(optionE);
                }
                String optionF = WorkbookUtil.getStringCellValue(row,COL_OPTION_F);
                if (StringUtils.isNotBlank(optionF)){
                    questionEntity.setOptionF(optionF);
                }
                String required = WorkbookUtil.getStringCellValue(row,COL_REQUIRED);
                if (StringUtils.isBlank(required)){
                    throw new Exception("必填属性不能为空");
                }else if ("是".equals(required)){
                    questionEntity.setRequired("1");
                }else if ("否".equals(required)){
                    questionEntity.setRequired("0");
                }else {
                    throw new Exception("必填属性请填是或否");
                }
                questionList.add(questionEntity);
            }catch (Exception e){
                errors.add("第"+rowIdx+"行:"+e.getMessage());
            }
        }
        if (errors.isEmpty() == false){
            throw new Exception(JSONArray.toJSONString(errors));
        }
        map.put("measurement",result);
        map.put("question",questionList);
        return map;
    }



}
